package com.go.where.manager.service.impl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.go.where.manager.common.Constants;
import com.go.where.manager.dao.CourseDao;
import com.go.where.manager.dao.TeacherDao;
import com.go.where.manager.dao.TeacherRelationDao;
import com.go.where.manager.page.BaseQuery.OrderType;
import com.go.where.manager.page.Pagination;
import com.go.where.manager.pojo.CourseInfo;
import com.go.where.manager.pojo.TeacherRelationInfo;
import com.go.where.manager.pojo.user.UserInfoVO;
import com.go.where.manager.service.CourseService;

@Service("courseService")
public class CourseServiceImpl implements CourseService {

	@Resource(name = "courseDao")
	private CourseDao courseDao;
	@Resource(name = "teacherRelationDao")
	private TeacherRelationDao teacherRelationDao;
	@Resource
	private TeacherDao teacherDao;

	@Override
	public CourseInfo queryByPrimartKey(String cid) {
		CourseInfo info = courseDao.getByKey(cid);
		return info;
	}

	@Override
	public Pagination<CourseInfo> queryPage(CourseInfo courseInfo) {
		if (StringUtils.isNotEmpty(courseInfo.getOrderBy())
				&& courseInfo.getOrderType() != null) {
			if (courseInfo.getOrderType() == OrderType.asc) {
				courseInfo.setPage_orderByAsc(courseInfo.getOrderBy());

			} else {
				courseInfo.setPage_orderByDesc(courseInfo.getOrderBy());
			}
		}
		Integer count = courseDao.totalCount(courseInfo);
		courseInfo.calculate();
		List<CourseInfo> list = courseDao.queryList(courseInfo);
		Pagination<CourseInfo> page = courseDao.queryPagination(count, list,
				courseInfo);
		return page;
	}

	@Override
	public int save(CourseInfo courseInfo, UserInfoVO currentUser) {
		String courseId = courseDao.getNextCourseKey();
		courseInfo.setCourseId(courseId);
		courseInfo.setPublishTime(new Date());
		courseInfo.setUserId(currentUser.getUserId());
		courseInfo.setIsDelete(Constants.NOT_DELETED);
		TeacherRelationInfo rela = new TeacherRelationInfo();
		rela.setId(UUID.randomUUID().toString());
		rela.setCourseId(courseId);
		rela.setTeacherId(courseInfo.getTeacherId());
		rela.setBeginTeachTime(courseInfo.getBeginTeachTime());
		rela.setEndTeachTime(courseInfo.getEndTeachTime());
		courseDao.insert(courseInfo);
		return teacherRelationDao.insert(rela);
	}

	@Override
	public int update(CourseInfo courseInfo, UserInfoVO currentUser) {
		courseInfo.setUpdateTime(new Date());
		courseInfo.setUserId(currentUser.getUserId());
		if(StringUtils.isBlank(courseInfo.getIsDelete())){
			TeacherRelationInfo rela = new TeacherRelationInfo();
			rela.setCourseId(courseInfo.getCourseId());
			rela.setTeacherId(courseInfo.getTeacherId());
			rela.setBeginTeachTime(courseInfo.getBeginTeachTime());
			rela.setEndTeachTime(courseInfo.getEndTeachTime());
			teacherRelationDao.updateByCourseId(rela);
		}
		return courseDao.update(courseInfo);
	}

	@Override
	public List<CourseInfo> queryAll() {
		return courseDao.queryAll();
	}

	@Override
	public String getCourseSubId() {
		return courseDao.getNextKey();
	}

}
